package com.czkt.spring_boot_thymeleaf.controller;

import com.czkt.spring_boot_thymeleaf.pojo.SysUser;
import com.czkt.spring_boot_thymeleaf.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
public class UserController {

    @Autowired
    SysUserService sysUserService;

    @RequestMapping("/doLogin")
    public Object doLogin(String usrName, String usrpassword, Model model, HttpSession session){
        SysUser sysUser=sysUserService.login(usrName,usrpassword);
        if (sysUser==null){
            model.addAttribute("message","账号密码错误!");
            return "login";
        }else {
            session.setAttribute("user",sysUser);
            return "main";
        }
    }
    @GetMapping("/list")
    @ResponseBody
    public Object index(){
        return sysUserService.list();
    }
}
